package cn.edu.jiangnan.cashmanagement.service.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import cn.edu.jiangnan.cashmanagement.common.dao.CommonDao;
import cn.edu.jiangnan.cashmanagement.entity.UserEntity;


@Service
public class UserServiceImpl implements IUserService {

	@Autowired
    private CommonDao<UserEntity> userDao;

    @Autowired
    private JavaMailSender javaMailSender;
    
    UserServiceImpl(){
    }
/*    
    @Inject
    UserServiceImpl(UserDao userDao,JavaMailSender javaMailSender) {
        this.userDao = userDao;
        this.javaMailSender = javaMailSender;
        if (this.javaMailSender != null) {
            this.sendMailExeService = Executors.newFixedThreadPool(5);
        }
    }
*/
	@Override
	public UserEntity getUser(Long userId) {
		userDao.setClazz(UserEntity.class);
		return userDao.get(userId);
	}
	
	@Override
	public List<UserEntity>  findUserByCondition(Map mp){
		StringBuffer hql = new StringBuffer("from UserEntity u where 1=1 ");
		if(null!=mp.get("userName")&&StringUtils.hasLength(mp.get("userName").toString())){
			hql.append(" and u.userName like '%"+mp.get("userName")+"%'");
		}
		
		if(null!=mp.get("trueName")&&StringUtils.hasLength(mp.get("trueName").toString())){
			hql.append(" and u.trueName like '%"+mp.get("trueName")+"%'");
		}
		
		if(null!=mp.get("departmentName")&&StringUtils.hasLength(mp.get("departmentName").toString())){
			hql.append(" and u.department.departmentId ='"+mp.get("departmentName")+"'");
		}
		return userDao.getListForPage(hql.toString(),0,0);
	}
	
    private ExecutorService sendMailExeService;

    class SendMailRunnable implements Runnable {
        private MimeMessage message;

        private JavaMailSender mailSender;
        
        SendMailRunnable(MimeMessage message  ,JavaMailSender mailSender) {
            this.message = message;
            this.mailSender = mailSender;
        }

        public void run() {
            try {
                if (mailSender != null && message != null) {
                    mailSender.send(message);
                }
            } catch (Throwable zzz) {
                zzz.printStackTrace(); 
            }
        }
    }

	@Override
	public boolean deleteUsers(List<Long> ids) {
		userDao.setClazz(UserEntity.class);
		if(null!=ids&&ids.size()>0){
			return userDao.deleteAll(ids);
		}else{
			return true;
		}
	}
	
	@Override
	public Long saveOrUpdateUserInfo(UserEntity user) {
		if(null!=user.getUserId()){//update
			
		}else{//save
			
		}
		return null;
	}
	
	@Override
	public boolean deleteUserById(String idStr) {
		Assert.notNull(idStr);
		Assert.hasLength(idStr);
		String[] idArr = idStr.split(",");
		List<Long> ids = new ArrayList<Long>(idArr.length);
		for(String id:idArr){
			if(StringUtils.hasText(id)){
				ids.add(Long.valueOf(id));
			}
		}
		return userDao.deleteAll(ids);
	}
}
